package com.zcm.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.zcm.entity.user;
import com.zcm.service.UserService;

/**
 * Servlet implementation class DoLoginservlet
 */
@WebServlet("/DoRegistServlet")
public class DoRegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public DoRegistServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		UserService us = new UserService();
		user user = new user();
		String username = request.getParameter("username");
		user.setUsername(username);
		user.setPassword(request.getParameter("password"));
		user.setName(request.getParameter("name"));

		if (us.isExit(username) > 0) {
			request.setAttribute("msg", "用户名已被注册");
			request.getRequestDispatcher("/regist.jsp").forward(request, response);

		} else {
			us.AddUser(user);
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
